package 数组.多数元素;

import java.util.Arrays;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/22 15:21
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int[] array = {2,2,1,1,1,2,2};
        System.out.println(majorityElement(array));
    }

    //双指针
    public static int majorityElement(int[] nums) {
        int start = 0;
        int end = 0;
        Arrays.sort(nums);
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i-1]){
                end = i;
            }
            if (nums[i] != nums[i-1]){
                if (end - start >= nums.length/2){
                    return nums[end];
                }else {
                    start = i;
                }
            }
        }
        return nums[end];
    }

    //也可以使用hash统计个数，然后找出最大的value对应的KEY

}
